package com.example.yiqinmanagersystem.controller.page;

import com.alibaba.fastjson.JSONObject;
import com.example.yiqinmanagersystem.entity.ChinaToal;
import com.example.yiqinmanagersystem.entity.NocvNews;
import com.example.yiqinmanagersystem.service.ChinaTotalService;
import com.example.yiqinmanagersystem.service.NocvNewsService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/*
 **@Author:youpeng
 **Created on 2022/9/11
 */
@Controller
@RequestMapping("/page")
public class PageDataDisplayController {

    @Resource
    private ChinaTotalService chinaTotalService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private NocvNewsService nocvNewsService;

    @RequestMapping("/")
    public String index(Model model){
        ChinaToal chinaToal = chinaTotalService.getById(1);
        model.addAttribute("chinaTotal",chinaToal);
        return "index";
    }
    /*
     * 获取实时疫情数据
     * */
    @GetMapping("/toMap")
    public String toMap(Model model){
        //redis 查库数据
        if(redisTemplate!=null){
            Object o = redisTemplate.opsForValue().get("chinaTotal");
            //缓存有数据
            if(o!=null){
                ChinaToal chinaToalData = JSONObject.parseObject(o.toString(), ChinaToal.class);
                model.addAttribute("chinaTotal",chinaToalData);
                //查询疫情公告数据
                List<NocvNews> newLists = nocvNewsService.listNewsLimit5();
                model.addAttribute("newsList",newLists);
//                System.out.println("查redis");
                return "china";
            }else {
                //缓存无数据
                ChinaToal chinaToal = chinaTotalService.getById(1);
                model.addAttribute("chinaTotal",chinaToal);
//                System.out.println("查mysql");
                redisTemplate.opsForValue().set("chinaTotal",chinaToal,10, TimeUnit.SECONDS);
                //查询疫情公告数据
                List<NocvNews> newLists = nocvNewsService.listNewsLimit5();
                model.addAttribute("newsList",newLists);
                return "china";
            }
        }

        ChinaToal chinaToal = chinaTotalService.getById(1);
        model.addAttribute("chinaTotal",chinaToal);
        //查询疫情公告数据
        List<NocvNews> newLists = nocvNewsService.listNewsLimit5();
        model.addAttribute("newsList",newLists);
        return "china";
    }

    @GetMapping("/toPie")
    public String toPie(){
        return "pie";
    }

    @GetMapping("/toBar")
    public String toBar(){
        return "bar";
    }

    @GetMapping("/toLine")
    public String toLine(){
        return "line";
    }

    @GetMapping("/toGlobalMap")
    public String toGlobalMap(){
        return "global";
    }

}
